package com.cpo.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.cpo.dao.UserRegistrationDao;
import com.cpo.domain.User;
import com.cpo.interfaces.IUserRegistration;



@Controller
@Scope(value="session")
public class UserRegistrationController {

	@Autowired
	private IUserRegistration registrationDao;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) 
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@RequestMapping(value="/registerUser", method=RequestMethod.GET)
	public ModelAndView newuserForm()
	{
		ModelAndView mav = new ModelAndView("UserRegistration");
		User user = new User();
		mav.getModelMap().put("UserRegistration", user);
		return mav;
	}
	
	@RequestMapping(value="/registerUser", method=RequestMethod.POST)
	public String create(@ModelAttribute("UserRegistration")User user, BindingResult result, SessionStatus status)
	{
		/*validator.validate(contact, result);
		if (result.hasErrors()) 
		{				
			return "newContact";
		}*/
		
		registrationDao.save(user);
		status.setComplete();
		return "redirect:home.do";
	}
}
